package org.teemor.demo.netty;

import org.junit.Test;
import org.teemor.demo.netty.client.TimeClient;
import org.teemor.demo.netty.server.time.TimeServer;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;


public class NettyApplication {

    public static void main(String[] args) throws InterruptedException {

        new Thread(new Runnable() {
            @Override
            public void run() {
                int port = 8080;
                try {
                    new TimeServer(port).run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int port = 8080;
                try {
                    new TimeClient("127.0.0.1", port).run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("server start");

    }


    @Test
    public void testDiscardServer() throws InterruptedException {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                int port = 8080;
//                try {
//                    new DiscardServer(port).run();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//        System.out.println("server start");
//        Thread.sleep(2000);
        List<FutureTask<Object>> futureTasks = new ArrayList<>();
        ExecutorService executorService = Executors.newFixedThreadPool(50);
        CountDownLatch count = new CountDownLatch(50);
        for (int i = 0; i < 1000; i++) {
            final int index = i;
            FutureTask<Object> futureTask = new FutureTask<>(new Callable<Object>() {
                @Override
                public String call() throws Exception {
                    Socket socket = new Socket("localhost", 8080);
//                    SocketAddress address = new InetSocketAddress("127.0.0.1", 8080);
//                    socket.bind(address);
                    OutputStream outputStream = socket.getOutputStream();

                    DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                    dataOutputStream.writeUTF("test" + index);
                    dataOutputStream.flush();
                    socket.shutdownOutput();
//                    Thread.sleep(10000);
                    InputStream inputStream = socket.getInputStream();
                    BufferedReader dataInputStream = new BufferedReader(new InputStreamReader(inputStream));

                    String res = null;
                    while ((res = dataInputStream.readLine()) != null) {
                        System.out.println("socket## - " + res);
                    }


                    dataOutputStream.close();
                    dataInputStream.close();
                    socket.close();
                    count.countDown();
                    return null;
                }
            });
            executorService.submit(futureTask);
        }
        count.await();
    }
}
